
//
//  UIView+OneDay.swift
//  OneDay
//
//  Created by cheng xi on 2016/10/5.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

enum TXOscillatoryAnimationType {
  case toBigger
  case toSmaller
  case toVoice
}

extension UIView {

  class func showOscillatoryAnimationWithLayer(_ layer:CALayer,WithType type:TXOscillatoryAnimationType,AndBlock block:(()->())? = nil ){
    
    var scale_1:NSNumber! = nil
    var scale_2:NSNumber! = nil
    switch type {
    case .toBigger:
      scale_1 = 1.15
      scale_2 = 0.92
      break
    case .toSmaller:
      scale_1 = 0.5
      scale_2 = 1.15
      break
    case .toVoice:
      scale_1 = 1.10
      scale_2 = 0.95
      break
    }
    
    UIView.animate(withDuration: 0.15, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
        layer.setValue(scale_1, forKeyPath: "transform.scale")
      }) { (_) in
        UIView.animate(withDuration: 0.15, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: { 
          layer.setValue(scale_2, forKeyPath: "transform.scale")
          if let b = block {
            b()
          }
          }, completion: {
            _ in
            UIView.animate(withDuration: 0.1, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
              layer.setValue(1.0, forKeyPath: "transform.scale")
              }, completion:nil)
        })
    }
    
  }

}
